package com.feizhi.inventory.fragment.Inventorydetails.alreadyinventoryfragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aserbao.aserbaosandroid.functions.database.greenDao.db.DaoSession;
import com.aserbao.aserbaosandroid.functions.database.greenDao.db.TaskDetailsDao;
import com.feizhi.inventory.MainActivity;
import com.feizhi.inventory.R;
import com.feizhi.inventory.bean.InventoryDetailsBean;
import com.feizhi.inventory.database.TaskDetails;
import com.feizhi.inventory.database.dbApp;
import com.feizhi.inventory.databinding.FragmentAlreadyInventoryBinding;
import com.feizhi.inventory.fragment.Inventorydetails.AlreadyInventoryAdapter;
import com.feizhi.inventory.util.ThreadPool;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 已盘点
 * */
public class AlreadyInventoryFragment extends Fragment {

    private List<InventoryDetailsBean> data;

    private AlreadyInventoryAdapter adapter;
    private FragmentAlreadyInventoryBinding mBinding;

    private MainActivity mActivity;
    List<TaskDetails> taskDetailsList = new ArrayList<>();
    private TaskDetailsDao taskDetailsDao;
    List<TaskDetails> lists;
    private String taskID;
    private DaoSession daoSession;
    private List<TaskDetails> saoCodeList;

    public AlreadyInventoryFragment() {

    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (MainActivity) getActivity();
        daoSession = ((dbApp) mActivity.getApplication()).getDaoSession();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentAlreadyInventoryBinding.inflate(inflater);
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        setOnClickListener();
    }

    private void setOnClickListener() {
        adapter.setItemOnClickListener(new AlreadyInventoryAdapter.OnClickListener() {

            @Override
            public void OnItemClickListener(int position) {

                Bundle bundle = new Bundle();
                bundle.putString("id", taskDetailsList.get(position).getId());
                bundle.putString("name", taskDetailsList.get(position).getName());
                bundle.putString("spec", taskDetailsList.get(position).getSpec());
                bundle.putString("eps_id", taskDetailsList.get(position).getEpcID());
                bundle.putString("save_place", taskDetailsList.get(position).getSavePlace());
                bundle.putString("custodian", taskDetailsList.get(position).getCustodian());
                bundle.putString("department", taskDetailsList.get(position).getDepartment());
                bundle.putString("state", taskDetailsList.get(position).getState());
                bundle.putString("notes_1", taskDetailsList.get(position).getNotes_1());
                bundle.putString("notes_2", taskDetailsList.get(position).getNotes_2());
                bundle.putString("notes_3", taskDetailsList.get(position).getNotes_3());
                bundle.putString("type", taskDetailsList.get(position).getType());
                bundle.putString("inputDate", taskDetailsList.get(position).getInputDate());
                Navigation.findNavController(mActivity, R.id.fragment_my).navigate(R.id.itemDetailsFragment, bundle);
            }
        });
    }

    private void initView() {

        taskDetailsDao = daoSession.getTaskDetailsDao();


        LiveEventBus.get().with("search_condition1", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

                saoCodeList = taskDetailsDao.queryBuilder().where(TaskDetailsDao.Properties.TaskID.eq(taskID)
                ,TaskDetailsDao.Properties.State.eq("已盘点")).list();
                List<TaskDetails> condition = new ArrayList<>();
                taskDetailsList.clear();
                for (int i = 0; i < saoCodeList.size(); i++) {

                    if (saoCodeList.get(i).getName().contains(s)) {
                        taskDetailsList.add(saoCodeList.get(i));
                    }

                }
                int mateSize = taskDetailsList.size();
                Toast.makeText(mActivity, "为您搜索到" + mateSize + "条数据", Toast.LENGTH_SHORT).show();
                adapter.setData(taskDetailsList);

            }
        });

        LiveEventBus.get().with("Task_ID", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {

                taskID = s;
                onRefresh();
            }
        });


        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new AlreadyInventoryAdapter(taskDetailsList, mActivity);
        mBinding.recyclerView.setAdapter(adapter);

    }

    private void refresh() {

//        ProgressDialog progressDialog = new ProgressDialog(mActivity);
//        progressDialog.setTitle("加载数据");
//        progressDialog.setMessage("正在加载ing...");
//        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//        progressDialog.show();

        ThreadPool.runThread(() -> {
            lists = taskDetailsDao.queryBuilder().where(TaskDetailsDao.Properties.TaskID.eq(taskID),TaskDetailsDao.Properties.State.eq("已盘点")).list();
            if (!taskDetailsList.isEmpty()) {
                taskDetailsList.clear();
            }
            taskDetailsList.addAll(lists);
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.setData(taskDetailsList);
                    //      progressDialog.dismiss();
                }
            });
        });

    }

    public void onRefresh() {

        refresh();

    }
}